<?php
/**
 * @file
 * @author  Jakub Kraft <jakub.kraft@gmail.com>
 * @version 1.0
 */

class YoutubeOEmbedAdapter extends OEmbedAdapter {
	
	public static $pattern = '/^http\:\/\/www\.youtube\.com\/watch\?v\=/i';
	
	public function resolveURL($url) {
		# get data provided by youtube itself
		$curl_result = $this->_curlCall('http://www.youtube.com/oembed?url='.urlencode($url).'&format=json', false);
		
		if (is_string($curl_result['response']) && strlen($curl_result['response']) > 0) {
			$response = json_decode($curl_result['response'], true);
		}
		
		# add thumbnail_url for the video
		$parsed_url = parse_url($url);
		parse_str($parsed_url['query'], $parsed_query);
		$response['thumbnail_url'] = 'http://img.youtube.com/vi/'.$parsed_query['v'].'/default.jpg';
		
		if ($response) {
			return $response;
		} else {
			return array();
		}
	}
	
}